package com.ajc.seata.config;

import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @Author: AnJunchao
 * @Description:
 * @Date: Create in 2019-10-12 09:33:59
 * @Modified By:
 */
@Configuration
@EnableTransactionManagement
@DependsOn("transactionManager")
@EnableJpaRepositories(
        entityManagerFactoryRef="bank2ManagerFactory",
        transactionManagerRef="transactionManager",
        basePackages=  "com.ajc.seata.repository.bank2")
public class Bank2Config {
    @Autowired
    JpaProperties jpaProperties;
    @Bean
    @ConfigurationProperties(prefix = "spring.jta.atomikos.datasource.jta-bank2")
    @Qualifier("dataSourceJTABank2")
    public DataSource dataSourceJTABank2() {
        return new AtomikosDataSourceBean();
    }


    /*--------- atomikos -----------*/
    @Bean(name = "bank2ManagerFactory")
    @DependsOn("transactionManager")
    public LocalContainerEntityManagerFactoryBean bannk2ManagerFactoryDemo(EntityManagerFactoryBuilder builder) {
        //jpaProperties.getProperties().put("hibernate.transaction.jta.platform", AtomikosJtaPlatform.class.getName());
        return builder
                .dataSource(dataSourceJTABank2())
                .properties(jpaProperties.getProperties())
                .packages("com.ajc.seata.entity.bank2") //设置实体类所在位置：类或包
                .persistenceUnit("jta-bank2") //持久化单元名称
                .build();
    }

}
